
import tkinter
import tkinter.constants as TKC
import tkinter.ttk as ttk
import database #my own class for database functions.
#import UI #my own class. It is an empty class to hold tkinter UI variables, so that they are not in the global namespace.
import calendarHelp

application = tkinter.Tk()

DB = database.Database()

class CalendarFrame:
    CALENDARFRAME = ttk.Frame(application)
    CALENDARFRAME.pack()
    Weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

#Creates the labels for days of the week at the top of the calendar
    for i in range(7):
        header = ttk.Label(CALENDARFRAME, text=Weekdays[i])
        header.grid(row=0, column=i, padx=15, pady=13)

    def saveEvent():
        array = {}
        array["date"] = CalendarFrame.dateEntry.get()
        array["heading"] = CalendarFrame.headingEntry.get()
        array["details"] = CalendarFrame.detailsEntry.get('1.0','end') #The get method for the tkinter Text box is wierd, and has little documentation. see: http://www.tkdocs.com/tutorial/text.html
        try:
            array["location"] = CalendarFrame.locationEntry.get()
            array["begin_time"] = CalendarFrame.begin_time.get()
            array["end_time"] = CalendarFrame.end_time.get()
        except:
            pass
        #print(array)
        
        DB.addEvent(array)

    def showMore():
        #CalendarFrame.save.destroy()
        CalendarFrame.showmore.destroy()
        CalendarFrame.showless = ttk.Button(CalendarFrame.Frame1, text="Show less", command=CalendarFrame.showLess)
        CalendarFrame.showless.grid(row=3, column=2)
        CalendarFrame.moreDetailsFrame = ttk.Frame(CalendarFrame.dialog)
        CalendarFrame.moreDetailsFrame.pack(side=TKC.BOTTOM)
        CalendarFrame.locationLabel = ttk.Label(CalendarFrame.moreDetailsFrame, text="Location")
        CalendarFrame.locationLabel.grid()
        CalendarFrame.locationEntry = ttk.Entry(CalendarFrame.moreDetailsFrame)
        CalendarFrame.locationEntry.grid()
        CalendarFrame.invitees = ttk.Button(CalendarFrame.moreDetailsFrame, text = "Invitees")
        CalendarFrame.invitees.grid()
        CalendarFrame.begin_timeLabel = ttk.Label(CalendarFrame.moreDetailsFrame, text = "Begin Time")
        CalendarFrame.begin_timeLabel.grid()
        CalendarFrame.begin_time = ttk.Entry(CalendarFrame.moreDetailsFrame)
        CalendarFrame.begin_time.grid()
        CalendarFrame.end_timeLabel = ttk.Label(CalendarFrame.moreDetailsFrame, text = "End Time")
        CalendarFrame.end_timeLabel.grid()
        CalendarFrame.end_time = ttk.Entry(CalendarFrame.moreDetailsFrame)
        CalendarFrame.end_time.grid()
        CalendarFrame.save = ttk.Button(CalendarFrame.moreDetailsFrame, text="Save", command=CalendarFrame.saveEvent)
        CalendarFrame.save.grid()
            
    def showLess():
        CalendarFrame.save.destroy()
        CalendarFrame.showless.destroy()
        CalendarFrame.moreDetailsFrame.destroy()
        CalendarFrame.save = ttk.Button(CalendarFrame.Frame1, text="Save", command=CalendarFrame.saveEvent)
        CalendarFrame.save.grid(row=3, column=3)
        CalendarFrame.showmore = ttk.Button(CalendarFrame.Frame1, text="Show more", command=CalendarFrame.showMore)
        CalendarFrame.showmore.grid(row=3, column=2)

    def createEvent():
        CalendarFrame.dialog = tkinter.Toplevel(application)
        CalendarFrame.dialog.title("About this application...")
        CalendarFrame.Frame1 = ttk.Frame(CalendarFrame.dialog)
        CalendarFrame.Frame1.pack()
        CalendarFrame.dateLabel = ttk.Label(CalendarFrame.Frame1, text="Date:")
        CalendarFrame.dateLabel.grid(row=0, column=0)
        CalendarFrame.dateEntry = ttk.Entry(CalendarFrame.Frame1)
        CalendarFrame.dateEntry.grid(row=0, column=1)
        CalendarFrame.headingLabel = ttk.Label(CalendarFrame.Frame1, text="Heading:")
        CalendarFrame.headingLabel.grid(row=0, column =2)
        CalendarFrame.headingEntry = ttk.Entry(CalendarFrame.Frame1)
        CalendarFrame.headingEntry.grid(row=0, column=3)
        CalendarFrame.detailsLabel = ttk.Label(CalendarFrame.Frame1, text="Details")
        CalendarFrame.detailsLabel.grid(row=1, column = 0)
        CalendarFrame.detailsEntry = tkinter.Text(CalendarFrame.Frame1, height=4, width =50)
        CalendarFrame.detailsEntry.grid(row=2, column = 0, columnspan=4)
        CalendarFrame.save = ttk.Button(CalendarFrame.Frame1, text="Save", command=CalendarFrame.saveEvent)
        CalendarFrame.save.grid(row=3, column=3)
        CalendarFrame.showmore = ttk.Button(CalendarFrame.Frame1, text="Show more", command=CalendarFrame.showMore)
        CalendarFrame.showmore.grid(row=3, column=2)

    CH = calendarHelp.CalendarHelp()
    for i in range(7):  #Creates the first calendar row of dates and events
        row1 = ttk.Frame(CALENDARFRAME) #Each day is wrapped in a frame
        row1.grid(row=1, column = i)
        button1 = ttk.Button(row1, text=CH.getDate(i), command=createEvent) #This button shows the month and day. Clicking the button will allow user to create new event on that day.
        button1.pack()
        label1 = ttk.Label(row1, text=CH.getDate(i)) #Each event on a day is in a Label widget. clicking the Event allows the user to modify the event.
        label1.bind('<1>', lambda e: print('r'))
        label1.pack()

    for i in range(7):  #Creates the second calendar row of dates and events
        row2 = ttk.Frame(CALENDARFRAME) 
        row2.grid(row=2, column = i)
        label2 = ttk.Label(row2, text=CH.getDate(i+7)) 
        label2.pack()

application.mainloop()
